package com.sgb.kjwl.view.ui.fragment.home;

import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.GridLayoutManager;

import com.amap.api.maps.LocationSource;
import com.sgb.kjwl.AppFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FragmentNewSourceBinding;
import com.sgb.kjwl.di.home.DaggerHomeFragComponent;
import com.sgb.kjwl.di.home.HomeFragModule;
import com.sgb.kjwl.model.entity.manage.TheEnterpriseOfEntity;
import com.sgb.kjwl.view.ui.activity.manage.ShopInfoForCompanyTradesActivity;
import com.sgb.kjwl.view.ui.adapter.manage.RecommendedEnterprisesAdapter;
import com.sgb.kjwl.viewmodel.manage.EnterpriseManageChildFragModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;

import java.util.List;

import javax.inject.Inject;

/**
 * 2020-11-05  产品马佳
 * Description:
 * Author CFK
 */
public class EnterpriseManageChildFragment extends AppFragment implements LocationSource.OnLocationChangedListener {
    @Inject
    EnterpriseManageChildFragModel model;
    private FragmentNewSourceBinding binding;
    public static final String KEY_TYPE = "type";
    private RecommendedEnterprisesAdapter recommendedEnterprisesAdapter;
    private String longitude;
    private String latitude;
    private int position;


    public static EnterpriseManageChildFragment newInstance(String longitude, String latitude, int position) {
        EnterpriseManageChildFragment fragment = new EnterpriseManageChildFragment();
        Bundle bundle = new Bundle();
        bundle.putString("longitude", longitude);
        bundle.putString("latitude", latitude);
        bundle.putInt("position", position);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        DaggerHomeFragComponent.builder()
                .appComponent(appComponent)
                .homeFragModule(new HomeFragModule(this))
                .build()
                .inject(this);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_new_source, container, false);
        return binding.getRoot();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        init();
    }

    private void initView() {
        longitude = getArguments().getString("longitude");
        latitude = getArguments().getString("latitude");
        position = getArguments().getInt("position", 0);
        //首页 推荐
        if (recommendedEnterprisesAdapter == null) {
            recommendedEnterprisesAdapter = new RecommendedEnterprisesAdapter(getActivity(), null);
        }
        binding.rlvOption.setLayoutManager(new GridLayoutManager(getActivity(), 1));
        binding.rlvOption.setAdapter(recommendedEnterprisesAdapter);
        recommendedEnterprisesAdapter.setOnItemClick(new AdapterOnItemClick<TheEnterpriseOfEntity>() {
            @Override
            public void onItemClick(TheEnterpriseOfEntity entity, int position) {
                Intent intent = new Intent(getActivity(), ShopInfoForCompanyTradesActivity.class);
                if (entity.getSupplierId() != null) {
                    intent.putExtra("id", entity.getSupplierId());
                    startActivity(intent);
                } else {
                    Toast.makeText(getActivity(), "店铺id为空", Toast.LENGTH_SHORT).show();
                }
                ;
            }
        });
    }


    public void init() {
        if (position == 0) {
            model.getRecommendedList(longitude, latitude);
        } else {
            model.getNewestComList(longitude, latitude);
        }
    }

    public void showOptionView(List<TheEnterpriseOfEntity> datas) {
        if (datas.isEmpty()) {
            binding.ntscrollView.setVisibility(View.VISIBLE);
            binding.empty.setVisibility(View.VISIBLE);
            binding.rlvOption.setVisibility(View.GONE);
        }
        recommendedEnterprisesAdapter.setDatas(datas);
    }


    public boolean isFirstView() {
        if (null != binding && null != binding.rlvOption && null != binding.rlvOption.getChildAt(0)) {
            if (binding.rlvOption.getChildAt(0).getTop() < binding.rlvOption.getPaddingTop()) {
                return false;
            }
            return true;
        }
        return false;
    }

    @Override
    public void onLocationChanged(Location location) {

    }
}
